package com.xy.tool.util;

import java.text.DecimalFormat;

/**
 * @author Greekn Zhang
 * @date 2021/4/27 13:26
 * @description 文件大小单位转换工具类
 */
public class ConvertSizeUtils {
    private ConvertSizeUtils (){}

    public static String readableFileSize(long size) {
        if (size <= 0) {
            return "0";
        }
        final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
}
